package com.example.demo5;
/**
 * @author 张成
 * @date 2022/3/22 10:39
 * @version 1.0
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
/**
 * @author 张成
 * @date 2022/3/22 10:39
 * @version 1.0
 */
@WebServlet(name = "SessionServlet2", value = "/SessionServlet2")
public class SessionServlet2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("会话状态=" + (session.isNew() ? "新会话" : "旧会话") + "<br>");
        out.println("会话 ID=" + session.getId() + "<br>");
        long creationTime = session.getCreationTime();
        out.println("创建时间=" + new Date(creationTime) + "<br>");
        long lastAccessedTime = session.getLastAccessedTime();
        out.println("最近访问时间=" + new Date(lastAccessedTime) + "<br>");
        out.println("最大不活动时间=" + session.getMaxInactiveInterval() + "<br>");
        out.println("Cookie= " + request.getHeader("Cookie") + "<br>");
        out.println("现在时间= " + session.getAttribute("time") + "<br>");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
